package com.rest.web;


import com.rest.common.model.Message;
import com.rest.common.model.RestResponse;
import com.rest.common.validate.ValidateGroup;
import com.rest.model.User;
import com.rest.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value="创建用户", httpMethod = "POST" )
    @ApiImplicitParams(@ApiImplicitParam(name = "entity", required = true, dataType = "User", paramType = "body"))
    @PostMapping
    public RestResponse<User> createUser(@RequestBody @Validated({ValidateGroup.Create.class}) User entity) {
        return new RestResponse<User>(true,Message.SUCCESS,userService.save(entity));
    }

    @ApiOperation(value="删除用户", httpMethod = "DELETE" )
    @DeleteMapping("/{id}")
    public RestResponse<String> deleteUser(@PathVariable(value = "id")String id) {
        userService.deleteUserById(id);
        return new RestResponse<String>(true,Message.SUCCESS,"delete info");
    }

    @ApiOperation(value="更新用户", httpMethod = "PATCH" )
    @ApiImplicitParams(@ApiImplicitParam(name = "entity", required = true, dataType = "User", paramType = "body"))
    @PatchMapping("/{id}")
    public RestResponse<User> updateUser(@RequestBody @Validated({ValidateGroup.Update.class}) User entity) {
        return new RestResponse<User>(true,Message.SUCCESS,userService.updateUser(entity));
    }

    @ApiOperation(value="ID查询用户", httpMethod = "GET" )
    @ApiImplicitParams(@ApiImplicitParam(name = "id", required = true, dataType = "String", paramType = "path"))
    @GetMapping("/{id}")
    public RestResponse<User> getUser(@PathVariable(value = "id")String id) {
        return new RestResponse<User>(true,Message.SUCCESS,userService.findUserByUserId(id));
    }


    @ApiOperation(value="获取用户列表", httpMethod = "GET" )
    @GetMapping(value = "/users")
    public RestResponse<List<User>> userList() {
        return new RestResponse<List<User>>(true,Message.SUCCESS,userService.findAll());
    }

}
